/**
 * 
 */
package com.asset.salesforce.controller.ajax;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.domain.Calendar;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.dto.CalendarDTO;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.EmployeeService;

// TODO: Auto-generated Javadoc
/**
 * The Class CalendarRestController.
 *
 * @author Hisham
 */
@Controller
public class CalendarRestController {
	
	/** The calendar service. */
	@Autowired
	private CalendarService calendarService;
	
	/** The company service. */
	@Autowired
	private CompanyService companyService;
	
	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/**
	 * Gets the calendar.
	 *
	 * @param request the request
	 * @param response the response
	 * @param session the session
	 * @return the calendar
	 */
	@RequestMapping(value = "/getCalendarList")
    public @ResponseBody List<CalendarDTO> getCalendar(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		Integer empId = Integer.parseInt(request.getParameter("empId"));
		Employee userDetails = (Employee) session.getAttribute("userDetails");
		List<Calendar> calendars = new ArrayList<Calendar>();
		if(empId==0){
			calendars = calendarService.getAllEventsForCompany(userDetails.getCompany().getCompanyName());
		}
		else{
			calendars = calendarService.getAllEventsForEmployee(empId);
		}
		
		List<CalendarDTO> calendarDTOs = new ArrayList<CalendarDTO>();
		if(calendars.size()>0){
			for (Calendar calendar : calendars) {
				CalendarDTO c = new CalendarDTO();
				 c.setId(calendar.getId());
				 c.setStartDate(calendar.getStartDate().toString());
				 c.setEndDate(calendar.getEndDate().toString());
				 c.setTitle(calendar.getTitle());
				 calendarDTOs.add(c);
			}
		}
		return calendarDTOs;
    }
	
	/**
	 * Gets the logged employee calendar.
	 *
	 * @param request the request
	 * @param response the response
	 * @param session the session
	 * @return the logged employee calendar
	 */
	@RequestMapping(value = "/getLoggedEmployeeCalendar")
    public @ResponseBody List<CalendarDTO> getLoggedEmployeeCalendar(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		Employee userDetails = (Employee) session.getAttribute("userDetails");
		List<Calendar> calendars = calendarService.getAllEventsForEmployee(userDetails.getId());
		List<CalendarDTO> calendarDTOs = new ArrayList<CalendarDTO>();
		if(calendars.size()>0){
			for (Calendar calendar : calendars) {
				CalendarDTO c = new CalendarDTO();
				 c.setId(calendar.getId());
				 c.setStartDate(calendar.getStartDate().toString());
				 c.setEndDate(calendar.getEndDate().toString());
				 c.setTitle(calendar.getTitle());
				 calendarDTOs.add(c);
			}
		}
		return calendarDTOs;
    }
}